package com.zdkj.subway.check.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdkj.admin.api.entity.SysUser;
import com.zdkj.common.core.util.SpringContextHolder;
import com.zdkj.subway.check.entity.SwParamLine;
import com.zdkj.subway.check.entity.SwParamStation;
import com.zdkj.subway.check.mapper.SwParamLineMapper;
import com.zdkj.subway.check.mapper.SwParamStationMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LineOrStation {
    static SwParamLineMapper swParamLineMapper;
    static SwParamStationMapper swParamStationMapper;
    static{
        swParamLineMapper=SpringContextHolder.getBean(SwParamLineMapper.class);
        swParamStationMapper=SpringContextHolder.getBean(SwParamStationMapper.class);
    }

    /**
     * 获取线路名称或者车站名称
     * @param lineName
     * @param stationName
     * @return
     */
    public static JSONObject getlinenameOrStationName(String lineName, String stationName){
        QueryWrapper<SwParamLine> queryWrapperLine=new QueryWrapper<>();
        queryWrapperLine.eq("name",lineName);
        List<SwParamLine> swParamLines = swParamLineMapper.selectList(queryWrapperLine);
        String lineCode="";
        String stationCode="";
        if(swParamLines!=null && swParamLines.size()>0){
            lineCode=swParamLines.get(0).getCode();

            QueryWrapper<SwParamStation> queryWrapperStation=new QueryWrapper<>();
            queryWrapperStation.eq("name",stationName);
            queryWrapperStation.eq("line_code",lineCode);
            List<SwParamStation> swParamStations = swParamStationMapper.selectList(queryWrapperStation);
            if(swParamStations!=null && swParamStations.size()>0){
                stationCode=swParamStations.get(0).getCode();
            }
        }
        JSONObject res=new JSONObject();
        res.put("lineCode",lineCode);
        res.put("stationCode",stationCode);
        return res;
    }


    /**
     * 区分是否是线网人员
     * @return
     */
    public static List getmanagementLine(){
        SysUserMapper sysUserMapper = SpringContextHolder.getBean(SysUserMapper.class);
        Map<String,Object> param=new HashMap<>();
        param.put("user_code", UserInfo.getUserCode());
        List<SysUser> sysUsers = sysUserMapper.selectByMap(param);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation); //管理的线路，站点
        List<String> lines = new ArrayList<>();
        if(StringUtils.isNotEmpty(managementLineStation)){
            for(int i=0;i<managementLineStations.size();i++){ //获取管理的所有线路
                JSONObject jsonObject = managementLineStations.getJSONObject(i);
                String line = jsonObject.getString("line");
                lines.add(line);
            }
        }
        return lines;
    }


    /**
     * 区分是否是线网人员
     * @return
     */
    public static List getLineOrManagementLine(){
        SysUserMapper sysUserMapper = SpringContextHolder.getBean(SysUserMapper.class);
        Map<String,Object> param=new HashMap<>();
        param.put("user_code", UserInfo.getUserCode());
        List<SysUser> sysUsers = sysUserMapper.selectByMap(param);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        //管理的线路，站点
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<String> lines = new ArrayList<>();
        if("[]".equals(managementLineStation)){
            lines.add(sysUser.getLineCode());
        }else {
            //获取每个线路对应的所有站
            for(int i=0;i<managementLineStations.size();i++){
                JSONObject jsonObject = managementLineStations.getJSONObject(i);
                String line = jsonObject.getString("line");
                lines.add(line);
            }
        }
        return lines;
    }

}
